package com.dynamic.mybatis.core.metadata;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.dynamic.mybatis.core.utils.StringUtils;
import org.apache.ibatis.ognl.Ognl;

import java.io.Serializable;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @Description
 * @Author xs
 */
public class MappedStatementFieldValidator implements Serializable {
    protected boolean enable = Boolean.TRUE;

    protected boolean unsigned;//无符号
    protected boolean required;//必填项
    protected boolean digits;//数字 整数
    protected Number min;//最小值
    protected Number max;//最大值
    protected boolean number;//数字 包括小数
    protected Number minLength;//最小长度
    protected Number maxLength;//最大长度
    //key = 枚举值， value = 枚举备注
    protected Map<Object, String> enums;//枚举值
    protected String datePattern;//时间格式化
    protected String regex; //正则表达式
    protected Pattern regexPattern;
    protected String ognl;//ognl表达式
    protected Object ognlExpression;
    protected String errorMsg;

    public boolean isUnsigned() {
        return unsigned;
    }

    public void setUnsigned(boolean unsigned) {
        this.unsigned = unsigned;
    }

    public boolean isRequired() {
        return required;
    }

    public void setRequired(boolean required) {
        this.required = required;
    }

    public boolean isDigits() {
        return digits;
    }

    public void setDigits(boolean digits) {
        this.digits = digits;
    }

    public Number getMin() {
        return min;
    }

    public void setMin(Number min) {
        this.min = min;
    }

    public Number getMax() {
        return max;
    }

    public void setMax(Number max) {
        this.max = max;
    }

    public boolean isNumber() {
        return number;
    }

    public void setNumber(boolean number) {
        this.number = number;
    }

    public Number getMinLength() {
        return minLength;
    }

    public void setMinLength(Number minLength) {
        this.minLength = minLength;
    }

    public Number getMaxLength() {
        return maxLength;
    }

    public void setMaxLength(Number maxLength) {
        this.maxLength = maxLength;
    }

    public Map<Object, String> getEnums() {
        return enums;
    }

    public void setEnums(Map<Object, String> enums) {
        this.enums = enums;
    }

    public String getDatePattern() {
        return datePattern;
    }

    public void setDatePattern(String datePattern) {
        this.datePattern = datePattern;
    }


    public String getRegex() {
        return regex;
    }

    public void setRegex(String regex) {
        this.regex = regex;
        if (StringUtils.isNotBlank(regex)) {
            this.regexPattern = Pattern.compile(regex);
        }
    }

    public Pattern getRegexPattern() {
        return regexPattern;
    }

    public String getOgnl() {
        return ognl;
    }

    public void setOgnl(String ognl) {
        this.ognl = ognl;
        if (StringUtils.isNotBlank(ognl)) {
            try {
                this.ognlExpression = Ognl.parseExpression(this.ognl);
            } catch (Exception e) {
                throw new MybatisPlusException("ognl表达式异常。", e);
            }
        }
    }

    public Object getOgnlExpression() {
        return ognlExpression;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }
}